package com.tech.connect.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.view.HLineDivider;
import com.tech.connect.R;
import com.tech.connect.activity.OtherInfoActivity;
import com.tech.connect.view.SideBar;

import java.util.ArrayList;
import java.util.List;

public class MainTabContactFragment extends BaseFragment {

    private BaseAdapter<String, BaseHolder> adapter;
    private RecyclerView recycler;
    private List<String> allUser = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_main_tab_contact, null);
        return view;
    }

    @Override
    protected boolean isShowHeadBar() {
        return true;
    }

    @Override
    protected boolean isFullStateBar() {
        return true;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getHeadBar().setTitle("通讯录").setRightIcon(R.drawable.icon_contact_add, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        initView(view);
        loadData();
        initAllCityAdapter();
    }

    private void initView(View view) {
        // View = view.findViewById(R.id.);
        recycler = view.findViewById(R.id.recycler);

        recycler.addItemDecoration(new HLineDivider());
        recycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
        SideBar sideBar = view.findViewById(R.id.sideBar);
        sideBar.setIndexListener(new SideBar.IndexListener() {
            @Override
            public void onIndexAt(String index) {
                showLog(index);
            }
        });
    }

    private void loadData() {

    }

    private void initAllCityAdapter() {
        if (adapter == null) {
            adapter = new BaseAdapter<String, BaseHolder>(R.layout.item_layout_contact, allUser, 5) {
                @Override
                protected void convert(BaseHolder helper, int position) {
//                    String s = allHotCity.get(0);
                    TextView tvHeader = helper.getView(R.id.tvHeader);
                    if (position == 0) {
                        tvHeader.setVisibility(View.VISIBLE);
                    } else {
                        tvHeader.setVisibility(View.GONE);
                    }
                    TextView tvUser = helper.getView(R.id.tvUser);
                    if (position == 0) {
                        tvUser.setText("阿大");
                    } else if (position == 1) {
                        tvUser.setText("毛华");
                    } else if (position == 2) {
                        tvUser.setText("李凡");
                    } else if (position == 3) {
                        tvUser.setText("杨刚");
                    } else if (position == 4) {
                        tvUser.setText("张须");
                    } else {
                        tvUser.setText("好朋友");
                    }
                }
            };
            adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    jump2Activity(OtherInfoActivity.class);
                }
            });
            recycler.setAdapter(adapter);
        }
        adapter.notifyDataSetChanged();
    }
}
